package com.util;

import com.models.User;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by TimeLiar on 2015/9/17.
 * All Hail The King !!!!!!
 */
public enum Constants {
    @SuppressWarnings("unused")
    INSTANCE;
    @SuppressWarnings("unused")
    public static final String CREATE_TABLE = "create table";
    @SuppressWarnings("unused")
    public static final String DROP_TABLE = "drop table";
    @SuppressWarnings("unused")
    public static final String ALTER_TABLE = "alter table";
    @SuppressWarnings("unused")
    public static final String INSERT = "insert";
    @SuppressWarnings("unused")
    public static final String DELETE = "delete";
    @SuppressWarnings("unused")
    public static final String UPDATE = "update";
    @SuppressWarnings("unused")
    public static final String SELECT = "select";
    @SuppressWarnings("unused")
    public static final String CREATE_INDEX = "create index";
    @SuppressWarnings("unused")
    public static final String DROP_INDEX = "drop index";
    @SuppressWarnings("unused")
    public static final String CREATE_VIEW = "create view";
    @SuppressWarnings("unused")
    public static final String DROP_VIEW = "drop view";
    @SuppressWarnings("unused")
    public static final String CREATE_USER = "create user";
    @SuppressWarnings("unused")
    public static final String GRANT = "grant";
    @SuppressWarnings("unused")
    public static final String REVOKE = "revoke";
    @SuppressWarnings("unused")
    public static final String ALL = "*";
    @SuppressWarnings("unused")
    public static final String EXIT = "exit";
    @SuppressWarnings("unused")
    public static final String INT = "int";
    @SuppressWarnings("unused")
    public static final String CHAR = "char";
    @SuppressWarnings("unused")
    public static final String VARCHAR = "varchar";
    @SuppressWarnings("unused")
    public static final String DOUBLE = "double";
    @SuppressWarnings("unused")
    public static final String DATE = "date";
    @SuppressWarnings("unused")
    public static final String DATETIME = "datatime";
    @SuppressWarnings("unused")
    public static final String TEXT = "text";
    @SuppressWarnings("unused")
    public static final String BOOLEAN = "boolean";

    public static Properties properties;
    static{
        try {
            properties = new Properties();
            properties.load(Constants.class.getClassLoader().getResourceAsStream("system.properties"));
            String systemHome = properties.getProperty("system-data-home");
            File userData = new File(systemHome+properties.getProperty("user-data"));
            if(!userData.exists()){
                System.out.println("create user file directory");
                if(!userData.mkdirs()){
                    System.err.println("create user file directory failed");
                    System.exit(0);
                }
            }
            File tableHome = new File(systemHome+properties.getProperty("table-prefix"));
            if(!tableHome.exists()){
                System.out.println("create table file directory");
                if(!tableHome.mkdirs()){
                    System.err.println("create table file directory failed");
                    System.exit(0);
                }
            }
            File indexHome = new File(systemHome+properties.getProperty("index-prefix"));
            if(!indexHome.exists()){
                System.out.println("create index file directory");
                if(!indexHome.mkdirs()){
                    System.err.println("create index file directory failed");
                    System.exit(0);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static final String SYS_HOME = properties.getProperty("system-data-home");
    public static final String USER_DATA = SYS_HOME + properties.getProperty("user-data")+"user.json";
    public static final String TABLE_HOME = SYS_HOME + properties.getProperty("table-prefix");
    public static final String INDEX_HOME = SYS_HOME + properties.getProperty("index-prefix");
    public static final String TABLE_DATA = TABLE_HOME + "*"+properties.getProperty("data-suffix");
    public static final String TABLE_STRUCTURE = TABLE_HOME + "*"+properties.getProperty("table-suffix");
    public static final String INDEX_TEMP = INDEX_HOME + "*"+properties.getProperty("index-suffix");
    public static final User SUPER_USER = new User();
    static{
        SUPER_USER.setUsername(properties.getProperty("super-admin"));
        SUPER_USER.setPassword(MD5.getMD5Code(properties.getProperty("super-password")));
        Map<String,String> root = new HashMap<>();
        root.put("*","*");
        SUPER_USER.setPermission(root);
    }

}
